package model

import (
	"errors"
	"gorm.io/gorm"
	"time"
)

type Product struct {
	Id          uint       `json:"id"`
	Name        string     `json:"name"`
	Price       float32    `json:"price"`
	Stock       int        `json:"stock"`
	Description string     `json:"description"`
	Detail      string     `json:"detail"`
	CreatedTime *time.Time `json:"created_time"`
	UpdatedTime *time.Time `json:"updated_time"`
	DeletedTime *time.Time `json:"deleted_time"`
	Version     int        `json:"version"`
}

func (Product) TableName() string {
	return "product"
}

func (p *Product) AfterSave(tx *gorm.DB) (err error) {
	if p.Stock < 0 {
		return errors.New("库存不足")
	}

	return nil
}
